package com.ruoyi.system.async;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.enums.CrawlerStatusType;
import com.ruoyi.common.enums.CustomerType;
import com.ruoyi.common.enums.ImportType;
import com.ruoyi.system.domain.CrmCustomerInfo;
import com.ruoyi.system.domain.CrmCustomerUserInfo;
import com.ruoyi.system.domain.vo.CustomerDTO;
import com.ruoyi.system.mapper.CrmCustomerInfoMapper;
import com.ruoyi.system.service.ICrmCustomerInfoService;
import com.ruoyi.system.service.ICrmCustomerUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
@Slf4j
public class ModifyMasterData {

    @Autowired
    private ICrmCustomerInfoService crmCustomerInfoService;

    @Autowired
    private CrmCustomerInfoMapper crmCustomerInfoMapper;

    @Autowired
    private ICrmCustomerUserService iCrmCustomerUserService;


    public void startScreening(Long customerId,String reptileType) {
        log.info("开启进行公司id:" + customerId + "的数据解析");
        CrmCustomerInfo crmCustomerInfo = crmCustomerInfoService.getById(customerId);
        CustomerDTO customerDTO = crmCustomerInfoMapper.selectAnalyst(customerId);
        List<CrmCustomerUserInfo> crmCustomerUserInfos = iCrmCustomerUserService.list(new QueryWrapper<CrmCustomerUserInfo>().eq("customer_id", customerId).isNotNull("identity_check_msg"));
        crmCustomerInfo.setInsuredPopulation(customerDTO.getInsuredPopulation());
        crmCustomerInfo.setQualifiedFollowers(customerDTO.getDeductionPloyment()+customerDTO.getUnemPloyment()+customerDTO.getOutPoverty());
        crmCustomerInfo.setOutPoverty(customerDTO.getOutPoverty());
        crmCustomerInfo.setRefundPart(customerDTO.getRefundPart());
        crmCustomerInfo.setRefundRetiredPart(customerDTO.getRefundRetiredPart());
        crmCustomerInfo.setDeductionPart(customerDTO.getDeductionPart());
        crmCustomerInfo.setDeductionRetiredPart(customerDTO.getDeductionRetiredPart());
        crmCustomerInfo.setDeductionPloyment(customerDTO.getDeductionPloyment());
        if (CollectionUtils.isEmpty(crmCustomerUserInfos)){
            crmCustomerInfo.setUnemPloyment(customerDTO.getOutPoverty()*2);
            crmCustomerInfo.setRefundOutPart(customerDTO.getRefundPart()*2);
            crmCustomerInfo.setDeductionOutPart(customerDTO.getDeductionPart()*2);
        }else {
            crmCustomerInfo.setUnemPloyment(customerDTO.getUnemPloyment());
            crmCustomerInfo.setRefundOutPart(customerDTO.getRefundOutPart());
            crmCustomerInfo.setDeductionOutPart(customerDTO.getDeductionOutPart());
        }
        //查询4050的数据
        Integer integer = crmCustomerInfoMapper.SelectAnalusEligible(customerId);
        crmCustomerInfo.setEligiblePersons(integer);
        //9月社保
        Integer selectAnalusNine = crmCustomerInfoMapper.SelectAnalusNine(customerId);
        crmCustomerInfo.setSeptemberSocial(selectAnalusNine);
        //意向客户限制导入 签约客户无限导入
        if (reptileType.equals("import")){
            if (CustomerType.CONTRACTEDCUSTOMERS.getCustomerTypeCode().equals(crmCustomerInfo.getCustomerType())) {
                crmCustomerInfo.setDataProcessing(ImportType.TOBEIMPROTINGDATA.getImprotTypeCode());
            } else {
                crmCustomerInfo.setDataProcessing(ImportType.RESTRICTIMPORTDATA.getImprotTypeCode());
            }
            log.info("公司id"+ customerId +"导入的转换参数为:"+crmCustomerInfo);
        }else {
            crmCustomerInfo.setCrawlerStatus(CrawlerStatusType.NORMAL.getCrawlerStatusTypeCode());
            log.info("公司id"+ customerId +"爬虫后的转换参数为:"+crmCustomerInfo);
        }
        boolean updateById = crmCustomerInfoService.updateById(crmCustomerInfo);
        log.info("开启进行公司id:" + customerId + "的数据解析完成");
    }
}